﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.OMM.Receptor.Adapters.Delegates;
using Sinacor.OMM.Util;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Sinacor.OMM.Receptor.Adapters
{
    /// <summary>
    /// Adapter STMCom.dll
    /// </summary>
    public class STMAdapter : IDisposable
    {


        public Interop.STMCom.TSTMComClass _stmClient;

        #region Events

        public event Action OnAcceptStart;
        public event Action OnConnect;
        public event Action<String> OnDisconnect;
        public event OnErrorEventHandler OnError;
        public event OnReceiveDataEventHandler OnReceiveData;
        public event Action OnStarted;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        public STMAdapter()
        {
            _stmClient = new Interop.STMCom.TSTMComClass();

            _stmClient.OnAcceptStart += new Interop.STMCom.ISTMComEvents_OnAcceptStartEventHandler(_stmClient_OnAcceptStart);
            _stmClient.OnConnect += new Interop.STMCom.ISTMComEvents_OnConnectEventHandler(_stmClient_OnConnect);
            _stmClient.OnDisconnect += new Interop.STMCom.ISTMComEvents_OnDisconnectEventHandler(_stmClient_OnDisconnect);
            _stmClient.OnError += new Interop.STMCom.ISTMComEvents_OnErrorEventHandler(_stmClient_OnError);
            _stmClient.OnReceiveData += new Interop.STMCom.ISTMComEvents_OnReceiveDataEventHandler(_stmClient_OnReceiveData);
            _stmClient.OnStarted += new Interop.STMCom.ISTMComEvents_OnStartedEventHandler(_stmClient_OnStarted);
        
        }

        #endregion

        #region Properties

        /// <summary>
        /// User
        /// </summary>
        public String User
        {
            get { return _stmClient.Cas_Usuario; }
            set { _stmClient.Cas_Usuario = value; }
        }

        /// <summary>
        /// Password
        /// </summary>
        public String Password
        {
            get { return _stmClient.Cas_Senha; }
            set { _stmClient.Cas_Senha = value; }
        }

        /// <summary>
        /// Employee
        /// </summary>
        public String Employee
        {
            get { return _stmClient.Cas_Funcionario; }
            set { _stmClient.Cas_Funcionario = value; }
        }

        /// <summary>
        /// New password
        /// </summary>
        public String NewPassword
        {
            get { return _stmClient.Cas_NovaSenha; }
            set { _stmClient.Cas_NovaSenha = value; }
        }

        /// <summary>
        /// Filter
        /// </summary>
        public String Filter
        {
            get { return _stmClient.Filter; }
            set { _stmClient.Filter = value; }
        }

        /// <summary>
        /// Compressed
        /// </summary>
        public bool Compressed
        {
            get { return _stmClient.Compressed; }
            set { _stmClient.Compressed = value; }
        }

        /// <summary>
        /// Host
        /// </summary>
        public String Host
        {
            get { return _stmClient.Host; }
        }

        /// <summary>
        /// Ack
        /// </summary>
        public bool Ack
        {
            get { return _stmClient.Ack; }
            set { _stmClient.Ack = value; }
        }

        #endregion

        #region Events STMCom.dll

        /// <summary>
        /// STMCom.dll OnAcceptStart
        /// </summary>
        void _stmClient_OnAcceptStart()
        {
            if (OnAcceptStart != null)
                OnAcceptStart();
        }

        /// <summary>
        /// STMCom.dll OnConnect
        /// </summary>
        void _stmClient_OnConnect()
        {
            if (OnConnect != null)
                OnConnect();
        }

        /// <summary>
        /// STMCom.dll OnDisconnect
        /// </summary>
        /// <param name="Desc"></param>
        void _stmClient_OnDisconnect(string Desc)
        {
            _stmClient = null;

            if (OnDisconnect != null)
                OnDisconnect(Desc);
        }

        /// <summary>
        /// STMCom.dll OnError
        /// </summary>
        /// <param name="Error"></param>
        /// <param name="Msg"></param>
        /// <param name="Desc"></param>
        void _stmClient_OnError(int Error, string Msg, string Desc)
        {
            if (OnError != null)
                OnError(Error, Msg, Desc);
        }

        /// <summary>
        /// STMCom.dll OnReceiveData
        /// </summary>
        /// <param name="LastMsgId"></param>
        /// <param name="SPF_Header"></param>
        /// <param name="DataPtr"></param>
        /// <param name="DataSize"></param>
        void _stmClient_OnReceiveData(string LastMsgId, string SPF_Header, string DataPtr, int DataSize)
        {
            if (OnReceiveData != null)
                OnReceiveData(LastMsgId, SPF_Header, DataPtr, DataSize);
        }

        /// <summary>
        /// STMCom.dll OnStarted
        /// </summary>
        void _stmClient_OnStarted()
        {
            if (OnStarted != null)
                OnStarted();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Connect
        /// </summary>
        /// <returns></returns>
        public bool Connect()
        {
            bool ret;

            try
            {
                ret = _stmClient.Connect();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ret;
        }




        /// <summary>
        /// Start request
        /// </summary>
        /// <param name="MessageId"></param>
        public void StartRequest(String MessageId)
        {
            try
            {
                _stmClient.StartRequest(MessageId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Disconnect
        /// </summary>
        public void Disconnect()
        {
            try
            {
                if (_stmClient != null)
                {
                    _stmClient.Disconnect();
                    _stmClient = null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        

        #endregion


        #region IDisposable Members

        public void Dispose()
        {
            try
            {
                if (_stmClient != null)
                {
                    _stmClient.Disconnect();
                    Marshal.ReleaseComObject(_stmClient);
                    
                }

                _stmClient = null;

                GC.GetTotalMemory(false);
                GC.Collect();
                GC.WaitForPendingFinalizers();

                GC.Collect();

                GC.GetTotalMemory(true);
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }

        #endregion
    }
}
